#include <iostream>
#include <vector>
#include <string>
#include <ctime>
#include <cstdlib>
#include <pthread.h>
#include "headers/pet.h"
#include "headers/inv.h"
 
using namespace std;

int main_selection;
Inventory inv(10);
void updateScore(int hunger, int boredom);
int getScore();
int score = 0;

int main()
{
    
    do {

        cout << "\t\t*------------------*\n";
        cout << "\t\t|                  |\n";
        cout << "\t\t| Digital Pet v1.0 |\n";
        cout << "\t\t|                  |\n";
        cout << "\t\t*------------------*\n";
        cout << "\t\t|                  |\n";
        cout << "\t\t|   1: New Game    |\n";
        cout << "\t\t|   2: About       |\n";
        cout << "\t\t|   3: Quit        |\n";
        cout << "\t\t|                  |\n";
        cout << "\t\t*------------------*\n\n";
        cout << "\t\t  Selection: ";

        cin >> main_selection;
        cin.ignore();

        switch(main_selection) {

            case 1: {
                int cont;
                int pet_num;
                int pet_focus = 0;
                int min = 1;
                int max = 30;
                int feed_result;
                int work_time;
                int game_over;
                string action_choice;
                vector<Pet> pet_list;
                string name;
                
                Pet *pet;
            
                cout << string(50,'\n');
                
                do {
            
                    // Generate random number.
                    srand(static_cast<unsigned int>(time(0)));
                    int randnum = rand() % (max-min + 1) + min;
            
                    // New pet generated if none exist, or if randnum matches a single particular number in min-max range.
                    if (pet_list.size() == 0 || randnum == 5) {
                        cout << "A new pet appears!\n\n";
                        pet = new Pet;
                        pet->SetName("unnamed");
                        pet_list.push_back(*pet);
                    }
            
                    // Display list of pets and their statuses, update score, and increment time for each.
                    pet_num = 0;
                    vector<Pet>::iterator iter;
                    cout << "ID\tHunger\tBoredom\tName\n";
                    for (iter = pet_list.begin(); iter != pet_list.end(); ++iter) { 
                        cout << pet_num << "\t" << iter->GetHunger() << "\t";
                        cout << iter->GetBoredom() << "\t" << iter->GetName() << endl;
                        iter->Time();
                        updateScore(iter->GetHunger(),iter->GetBoredom());
                        pet_num++;
                    }
            
                    // Display action options.
                    cout << "\nActions:\n";
                    cout << "\tname(1) - Update Pet Name\n";
                    cout << "\tfeed(2) - Give Food [" << inv.GetFood() << "]\n";
                    cout << "\tplay(3) - Play\n";
                    cout << "\tswitch(4) - Switch Pet\n";
                    cout << "\twork(5) - Go to Work\n\n";
            
                    // Display input prompt.
                    cout << "[" << score << "] ";
                    sleep(1);
                    cout << pet_list.at(pet_focus).GetName() << ": ";
                    getline(cin,action_choice);
                    cout << string(50,'\n');
            
                    // Execute chosen action.
                    while (true) {
                        if (action_choice.empty()) { // No choice given.
                            cout << string(50,'\n');
                            break;
                        } else if (action_choice == "1" || action_choice == "name") { // Update name.
                            name = "";
                            while (true) {
                                if (name == "") {
                                    cout << "Enter a new name for " << pet_list.at(pet_focus).GetName() << ": ";
                                    getline(cin,name);
                                } else {
                                    break;
                                }
                            }
                            pet_list.at(pet_focus).SetName(name);
                            cout << string(50,'\n');
                            cout << "Pet " << pet_focus << " is now named " << name << "!\n\n";
                            break;
                        } else if (action_choice == "2" || action_choice == "feed") { // Give food.
                            feed_result = inv.UseFood(1);
                            if (feed_result == 1) {
                                cout << "No food left. Could not feed " << pet_list.at(pet_focus).GetName() << ".\n";
                            } else {
                                pet_list.at(pet_focus).Feed();
                                cout << pet_list.at(pet_focus).GetName() << " says \"om nom nom nom...\".\n\n";
                            }
                            break;
                        } else if (action_choice == "3" || action_choice == "play") { // Play with pet.
                            pet_list.at(pet_focus).Play();
                            cout << pet_list.at(pet_focus).GetName() << " says \"WEEEEEEEEEE\".\n\n";
                            break;
                        } else if (action_choice == "4" || action_choice == "switch") { // Switch focus.
                            pet_num = 0;
                            cout << "ID\tName\n";
                            for (iter = pet_list.begin(); iter != pet_list.end(); ++iter) { 
                                cout << pet_num << "\t" << iter->GetName() << endl;
                                pet_num++;
                            }       
                            cout << "Enter pet ID: ";
                            cin >> pet_focus;
                            cin.ignore();
                            while (true) {
                                if (pet_focus < pet_num) {
                                    break;
                                } else {
                                    cout << "Invalid choice. Try again: ";
                                    cin >> pet_focus;
                                    cin.ignore();
                                }
                            }
                            cout << string(50,'\n');
                            cout << "Attention shifted to " << pet_list.at(pet_focus).GetName() << "\n\n";
                            break;
                        } else if (action_choice == "5" || action_choice == "work") { // Go to work, so we can get more food.
                            cout << "Work for how long?: ";
                            cin >> work_time;
                            cin.ignore();
                            for (int j=0;j<work_time;j++) {
                                for (iter = pet_list.begin(); iter != pet_list.end(); ++iter) {
                                    iter->Time();
                                }
                            }
                            cout << string(50,'\n');
                            cout << "You went to work and bought " << inv.AddFood(work_time) << " food rations!\n\n";
                            break;
                        } else {
                            cout << string(50,'\n');
                            cout << "Invalid command.\n\n";
                            break;
                        }
                    }
            
                    // Check all pets for starvation and boredom.
                    for (iter = pet_list.begin(); iter != pet_list.end(); ++iter) {
                        int hunger = iter->GetHunger();
                        int boredom = iter->GetBoredom();
                        if (hunger >= 100) {
                            cout << string(50,'\n');
                            cout << iter->GetName() << " died of starvation. ";
                            cout << "You lose with a score of " << score << ".\n\n";
                            game_over = 1;
                            break;
                        } else if (boredom >= 100) {
                            cout << string(50,'\n');
                            cout << iter->GetName() << " died of boredom and neglect. ";
                            cout << "You lose with a score of " << score << ".\n\n";
                            game_over = 1;
                        }
                    }
                    if (game_over == 1) {
                        break;
                    }
                } while (true);
                break;
            }
            case 2: {
                cout << string(50,'\n');
                cout << "About code goes here.\n\n";
                break;
            }
        }
    } while (main_selection != 3);
    return 0;
}

void updateScore(int hunger, int boredom) {
    if (hunger == 0) {
        score += 15;
    } else {
        score += 10 / hunger;
    }
    if (boredom == 0) {
        score += 15;
    } else {
        score += 10 / boredom;
    }
}

int getScore() {
    return score;
}
